<html>
<head>
    <meta http-equiv="content-Type" content="text/html; charset=utf-8" />
    <title>统计求和</title>
    <hta:application id="app" applicationname="app" scroll="no" />
    <style type="text/css">
        * {
            font-size: 11pt;
        }
        
        body {
            background-color: #c8c8c8;
        }
        
        .name {
            width: 20px;
            word-break: keep-all;
            white-space: nowrap;
        }
        
        .input {
            width: 100%;
            height: 30%;
        }
        
        .output {
            width: 100%;
            height: 70%;
        }
        
        .watermark {
            color: gray;
        }
        
        table {
            width: 100%;
            height: 100%;
        }
        
        textarea {
            width: 100%;
            height: 100%;
            overflow-x: auto;
            overflow-y: auto;
        }
    </style>
    <script type="text/javascript">
        var input = null;
        var output = null;
        var param = null;

        function init() {
            input = document.getElementById('numbers');
            output = document.getElementById('result');
            param = document.getElementById('param');

            input.title = '此处输入数值序列，以逗号、空格、回车换行等非数字字符分隔，可以从其他软件（如Excel、Word、记事本等）复制数值序列粘贴到该文本框中';
            param.title = '此处输入相加后的总和';

            input.value = input.title;
            param.value = param.title;
            output.value = '';

            input.className = 'watermark';
            param.className = 'watermark';
        }

        function clearInput() {
            input.value = input.title;
            input.className = 'watermark';
        }

        function clearResult() {
            output.value = '';
        }

        function myfocus(_this) {
            if (_this.value == _this.title) {
                _this.value = '';
                _this.className = '';
            }
        }

        function myblur(_this) {
            if (trim(_this.value) == '') {
                _this.value = _this.title;
                _this.className = 'watermark';
            }
        }

        function trim(str) {
            return str.replace(/(^\s*)|(\s*$)/g, '');
        }

        function combine(arr, num) {
            var r = [];

            (function f(t, a, n) {
                if (n == 0) {
                    return r.push(t);
                }
                for (var i = 0, l = a.length; i <= l - n; i++) {
                    f(t.concat(a[i]), a.slice(i + 1), n - 1);
                }
            })([], arr, num);

            return r;
        }

        function find() {
            var numbers = [];

            var text = input.value;
            var array = text.split(/[^0-9.]/g);
            for (var i in array) {
                if (array[i] != '') {
                    numbers.push(array[i]);
                }
            }

            if (numbers.length > 0) {
                if (!/\d/g.test(param.value)) {
                    param.focus();
                    alert('请输入求和条件。');
                    return;
                }

                var sum = parseFloat(param.value);
                var max = numbers.length;
                var result = [];
                for (var m = 1; m <= max; m++) {
                    var ret = combine(numbers, m);
                    for (var i in ret) {
                        var s = 0;
                        for (var j in ret[i]) {
                            s += parseFloat(ret[i][j]);
                        }

                        if (s == sum) {
                            result.push(ret[i].join('  '));
                        }
                    }
                }

                if (result.length == 0) {
                    output.value = '没有符合条件的数值序列。';
                } else {
                    var resultArray = [];
                    resultArray.push('共找到' + result.length + '组符合条件的数值序列：');
                    for (var i in result) {
                        resultArray.push('【' + (parseInt(i) + 1) + '】' + result[i]);
                    }
                    output.value = resultArray.join('\r\n');
                }
            } else {
                input.focus();
                alert('请输入数值序列。');
            }
        }
    </script>
</head>
<body onload="init()">
    <table>
        <caption>
        </caption>
        <tr>
            <th class="name">
                数值序列
            </th>
            <td class="input">
                <textarea id="numbers" onfocus="myfocus(this)" onblur="myblur(this)"></textarea>
            </td>
        </tr>
        <tr>
            <th class="name">
                求和条件
            </th>
            <td>
                <input id="param" type="text" onfocus="myfocus(this)" onblur="myblur(this)" />
                <input type="button" value="统计" onclick="find()" />&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="button" value="清空序列" onclick="clearInput()" />
                <input type="button" value="清空结果" onclick="clearResult()" />
            </td>
        </tr>
        <tr>
            <th class="name">
                查找结果
            </th>
            <td class="output">
                <textarea id="result" readonly="readonly"></textarea>
            </td>
        </tr>
    </table>
</body>
</html>
